<template>
  <div>
    <h1>抽奖记录</h1>
    <!-- 条件搜索 -->
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="关键字">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入关键字"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loadRecordList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格数据 -->
    <div>
      <el-table :data="recordList" stripe style="width: 100%">
        <el-table-column prop="recordId" label="ID" width="50" />
        <el-table-column prop="userId" label="UID" width="50" />
        <el-table-column prop="userName" label="微信昵称" width="100" />
        <el-table-column prop="activeName" label="活动名称" width="100" />
        <el-table-column prop="userMobile" label="联系方式" width="110" />
        <el-table-column prop="activeType" label="活动类型" width="80" />
        <el-table-column prop="winName" label="是否中奖" width="80" />
        <el-table-column prop="winProd" label="奖品" width="100" />
        <el-table-column prop="createTime" label="时间" width="160" />
        <el-table-column prop="recordDesc" label="备注" width="120" />
        <el-table-column label="操作" />
      </el-table>
    </div>
    <!-- 分页 -->
    <div>
      <div class="demo-pagination-block">
        <el-pagination
          v-model:current-page="searchForm.pageNum"
          v-model:page-size="searchForm.pageSize"
          :page-sizes="[2, 4, 6, 8]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, ref } from "vue";
import { getJoinRecord } from "../api/api";

const recordList = ref([]);
const total = ref(0);
const searchForm = ref({
  pageNum: 1,
  pageSize: 2,
  typeId: null,
  keyWord: null,
});

const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  searchForm.value.pageSize = val;
  loadRecordList();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  searchForm.value.pageNum = val;
  loadRecordList();
};

const loadRecordList = () => {
  getJoinRecord(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      recordList.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};

onMounted(() => {
  searchForm.value.typeId = sessionStorage.getItem("activeId");
  loadRecordList();
});
</script>

<!-- 分页样式 -->
<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
</style>

<!-- 条件搜索样式 -->
<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>
